package com.ysd.lis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkQualityStage;
import com.ysd.lis.entity.WkQualityStageType;
import com.ysd.lis.entity.WkQualityTemplateRecord;
import com.ysd.lis.mapper.WkQualityStageMapper;
import com.ysd.lis.request.QualityStageListParams;
import com.ysd.lis.service.WkQualityStageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.lis.service.WkQualityTemplateRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 质量控制阶段表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-05-07
 */
@Service
public class WkQualityStageServiceImpl extends ServiceImpl<WkQualityStageMapper, WkQualityStage> implements WkQualityStageService {

    @Autowired
    WkQualityStageMapper wkQualityStageMapper;

    @Autowired
    WkQualityTemplateRecordService wkQualityTemplateRecordService;

    @Override
    public Result getQualityStageList(QualityStageListParams QualityStageListParams) {
        MPJLambdaWrapper<WkQualityStage> lambdaQueryWrapper = JoinWrappers.lambda(WkQualityStage.class)
                .selectAll(WkQualityStage.class)
                .selectAs(WkQualityStageType::getFileUrl, WkQualityStage::getFileUrl)
                .leftJoin(WkQualityStageType.class, WkQualityStageType::getId, WkQualityStage::getStageTypeId)
                .eq(WkQualityStage::getDelFlag, 0)
                .eq(WkQualityStage::getPlanId, QualityStageListParams.getPlanId());
        List<WkQualityStage> wkQualityStages = wkQualityStageMapper.selectJoinList(WkQualityStage.class, lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", wkQualityStages);
    }

    @Override
    public Result addQualityStage(WkQualityStage wkQualityStage) {
        boolean save = this.save(wkQualityStage);
        if (save) {
            return Result.succ(1, "插入成功！", save);
        } else {
            return Result.fail("插入失败！");
        }
    }

    @Override
    public Result editQualityStage(WkQualityStage wkQualityStage) {
        boolean save = this.updateById(wkQualityStage);
        if (save) {
            return Result.succ(1, "编辑成功！", save);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result updateStagePathUrl(String url, String id, String historyUrl) {
        LambdaUpdateWrapper<WkQualityStage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WkQualityStage::getStageFileUrl, url)
                .eq(WkQualityStage::getId, id);
        int update = wkQualityStageMapper.update(null, updateWrapper);
        if (StrUtil.isNotEmpty(historyUrl)) {
            WkQualityTemplateRecord wkQualityTemplateRecord = new WkQualityTemplateRecord();
            wkQualityTemplateRecord.setSourceType("WkQualityStageRecordType");
            wkQualityTemplateRecord.setSourceId(id);
            wkQualityTemplateRecord.setSourceFileUrl(historyUrl);
            wkQualityTemplateRecord.setTargetFileUrl(url);
            wkQualityTemplateRecordService.add(wkQualityTemplateRecord);
        }
        if (update == 0) {
            return Result.succ(1, "无更新数据", "");
        } else if (update > 0) {
            return Result.succ(1, "更新成功", "");
        } else {
            return Result.fail("更新失败");
        }
    }

    @Override
    public Result deleteQualityStage(List<String> ids) {
        boolean save = this.removeByIds(ids);
        if (save) {
            return Result.succ(1, "删除成功！", save);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
